package com.ljx.springboot003.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.springboot003.entity.Depart;
import com.ljx.springboot003.entity.Employee;
import com.ljx.springboot003.service.DepartService;
import com.ljx.springboot003.service.EmployeeService;
import com.ljx.springboot003.mapper.EmployeeMapper;
import com.ljx.springboot003.vo.EmployeeVO;
import com.ljx.springbootcommon.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author linjingxiong
* @description 针对表【employee】的数据库操作Service实现
* @createDate 2025-01-12 13:40:32
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
    implements EmployeeService{
    @Autowired
    private DepartService departService;
    // 1. 分页查询
    @Override
    public PageResult<Employee> findByPage(Integer page, Integer pageSize) {
        // 1. 开始分页
        Page<Employee> page1 = this.page(new Page<>(page, pageSize));
        
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    
    // 3. 条件带分页查询
    @Override
    public PageResult<Employee> search(Integer page, Integer pageSize, EmployeeVO vo) {
        // 1. 进行分页带条件查询
        // 1.1 封装查询条件
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getEName()), "e_name", vo.getEName());
        wrapper.eq(vo.getEAge()!=null&&vo.getEAge()!=0, "e_age", vo.getEAge());
        wrapper.eq(vo.getEDepart() != null && vo.getEDepart() != 0, "e_depart", vo.getEDepart());
        // 1.2 开始分页查询
        Page<Employee> page1 = this.page(new Page<>(page, pageSize), wrapper);
        // 1.3 处理员工所在部门名称
        page1.getRecords().forEach(employee -> {
            Depart depart = departService.getById(employee.getEDepart());
            employee.setDepartName(depart.getDpName());
        });
        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    // 添加学生
    @Override
    public boolean add(Employee employee) {
        return this.save(employee) ;
    }
}




